<template>
  <div>
    <el-dialog
      title="分配角色"
      :visible="showAssign"
      @close="closeFn"
      @open="openFn"
    >
      <el-checkbox-group v-model="userIds">
        <!-- label是要存储的值 和 userIds双向绑定-->
        <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
          item.name
        }}</el-checkbox>
      </el-checkbox-group>
      <div slot="footer">
        <el-button
          size="mini"
          @click="$emit('update:showAssign', false)"
        >取 消</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="saveAssignFn"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleListApi, updateUserRoleApi } from '@/api'
export default {
  name: 'VueAdminTemplateMasterAssignRole',
  props: {
    showAssign: {
      // 弹窗状态
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: null
    }
  },

  data() {
    return {
      list: [], // 角色列表
      userIds: [] // 用户角色的id
    }
  },

  mounted() {},
  created() {
    this.getRoleList()
  },

  methods: {
    // 获取所有角色列表
    async getRoleList() {
      const { rows } = await getRoleListApi({ page: 1, pagesize: 20 }) // 角色不会太多 20条即可
      this.list = rows
    },

    // 点击 -> 保存
    async saveAssignFn() {
      await updateUserRoleApi({ id: this.userId, roleIds: this.userIds })
      this.$message.success('修改角色成功！')
      this.$emit('update:showAssign', false)
    },

    // 弹窗打开触发
    async openFn() {
      // // 获取当前员工id所在的角色
      // const { roleIds } = await userInFormationApi(this.userId)
      // // 赋值给el渲染
      // this.userIds = roleIds
    },

    // 关闭弹窗触发
    closeFn() {
      this.$emit('update:showAssign', false)
      this.userIds = []
    }
  }
}
</script>

<style lang="scss" scoped></style>
